package com.bfly.mall.order.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.context.IpThreadLocal;
import com.bfly.core.context.UserThreadLocal;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.order.dao.OrderCartMapper;
import com.bfly.mall.order.entity.OrderCart;
import com.bfly.mall.order.dto.OrderCartDTO;
import com.bfly.mall.order.service.IOrderCartService;
import com.bfly.mall.product.entity.Product;
import com.bfly.mall.product.entity.ProductSku;
import com.bfly.mall.product.service.IProductService;
import com.bfly.mall.product.service.IProductSkuService;
import com.bfly.mall.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/4/19 19:05
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class OrderCartServiceImpl extends BaseServiceImpl<OrderCart, Integer> implements IOrderCartService {

    @Autowired
    private OrderCartMapper cartMapper;
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductSkuService skuService;

    @Override
    public List<OrderCart> getOrderCart(int userId) {
        return cartMapper.getOrderCart(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addToCart(OrderCartDTO cartDTO) {
        User user = UserThreadLocal.get();
        Product product = productService.get(cartDTO.getProductId());
        if (product == null || !product.isOnShelf()) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "该产品不存在或已下架!");
        }
        ProductSku sku = skuService.getSku(cartDTO.getProductId(), cartDTO.getSn());
        if (sku == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "该产品不存在或已下架!");
        }

        // 查询购物车中是否存在同样的产品 存在则累加数量 不存在则添加购物车中
        OrderCart cart = cartMapper.getOrderCartItem(user.getId(), cartDTO.getProductId(), cartDTO.getSn());
        if (cart != null) {
            cart.setBuyCount(cartDTO.getBuyCount());
            if (cart.getBuyCount() <= 0) {
                // 购买数量小于0 则从购物车中删除该产品
                super.remove(cart.getId());
            } else {
                super.edit(cart);
            }
        } else {
            cart = new OrderCart();
            cart.setBuyCount(cartDTO.getBuyCount());
            cart.setSn(cartDTO.getSn());
            cart.setProductId(cartDTO.getProductId());
            cart.setTemplateId(product.getFreightId());
            cart.setUserId(user.getId());

            cart.setCover(product.getCover());
            cart.setName(product.getName());
            cart.setSpeTitle(sku.getTitle());
            cart.setPrice(sku.getPrice());

            cart.setCreateDate(new Date());
            cart.setCreateIp(IpThreadLocal.get());
            super.save(cart);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(int userId, int productId, String sn) {
        OrderCart item = cartMapper.getOrderCartItem(userId, productId, sn);
        if (item == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的购物车信息!");
        }
        super.remove(item.getId());
    }

    @Override
    public int getProductCount(int userId) {
        return cartMapper.getProductCount(userId);
    }
}
